<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <script src="js/jquery.min.js"></script>
    <script src="js/sessionTools.js"></script>


    <title>个人中心</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            /* font-family: Arial, sans-serif;
            background-color: #f7f7f7;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh; */

            font-family: Arial, sans-serif;

            /* 添加渐变背景 */
            background: linear-gradient(to bottom, #4b6cb7, #182848);

            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            width: 800px;
            background-color: #fff;
            padding: 50px;
            border-radius: 5px;
            box-shadow: 0px 2px 10px rgba(0, 0, 0, 0.3);
        }

        .title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
        }

        .info {
            display: flex;
            margin-bottom: 30px;
        }

        .label {
            width: 100px;
            font-weight: bold;
        }

        .avatar {
            width: 200px;
            height: 200px;
            border-radius: 50%;
            margin-right: 50px;
        }

        .upload-avatar-form .form-group {
            margin-bottom: 20px;
        }

        .form-label {
            display: block;
            font-weight: bold;
            margin-bottom: 5px;
        }

        .form-input {
            width: 300px;
            height: 40px;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 10px;
        }

        .form-submit {
            display: inline-block;
            background-color: #f00;
            color: #fff;
            padding: 10px 30px;
            border-radius: 5px;
            text-decoration: none;
        }

        /* 新添加的样式 */
        .menu {
            display: flex;
            align-items: center;
            position: absolute;
            top: 20px;
            right: 20px;
        }

        .menu a {
            display: inline-block;
            margin-left: 10px;
            color: #333;
            text-decoration: none;
            font-size: 16px;
            font-weight: bold;
            padding: 10px;
            border-radius: 5px;
            background-color: #eee;
            transition: background-color 0.3s ease-in-out;
        }

        .menu a:hover {
            background-color: #999;
            color: #fff;
        }
    </style>
</head>

<body>



    <div class="container">

        <div class="menu">
            <a href="blog_list.html">博客主页</a>
            <a href="myblog_list1.html">我的主页</a>
            <a href="blog_add.html">写博客</a>
            <a href="blog_draft.html">草稿箱</a>
            <a href="Javascript:exit()">注销</a>

        </div>

        <div class="info">
            <img class="avatar" src="img/avatar.png" alt="头像" id="image">
            <!-- <div class="info-content">
        <div class="label">用户名：</div>
        <div class="value">james</div>
        <div class="label">邮箱：</div>
        <div class="value">james@example.com</div>
        <div class="label">手机号：</div>
        <div class="value">13333333333</div>
      </div> -->
        </div>
        <div class="upload-avatar-form">
            <div class="title">上传头像</div>
            <div>
                <div class="form-group">
                    <!-- <label class="form-label" for="avatar-file">选择文件：</label> -->
                    <input class="form-input" type="file" name="avatar-file" id="avatar-file" accept="image/*">
                </div>
                <div class="form-group">
                    <button class="form-submit" type="submit" onclick="myupload()">上传</button>
                </div>
            </div>
        </div>
        <div class="update-pwd-form">
            <div class="title">更新密码</div>
            <div>
                <div class="form-group">
                    <label class="form-label" for="old-pwd">旧密码：</label>
                    <input class="form-input" type="password" name="old-pwd" id="old-pwd">
                </div>
                <div class="form-group">
                    <label class="form-label" for="new-pwd">新密码：</label>
                    <input class="form-input" type="password" name="new-pwd" id="new-pwd" placeholder="请输入 6-8 位数新密码">
                </div>
                <div class="form-group">
                    <label class="form-label" for="confirm-pwd">确认密码：</label>
                    <input class="form-input" type="password" name="confirm-pwd" id="confirm-pwd">
                </div>
                <div class="form-group">
                    <button class="form-submit" type="submit" onclick="mysub()" id="submit">提交</button>
                </div>
            </div>
        </div>
    </div>

    <script>


        function mysub() {
            // 1. 获取标签
            var old_password = jQuery("#old-pwd");
            var new_password = jQuery("#new-pwd");
            var new_password2 = jQuery("#confirm-pwd");

            // 2. 数据效验
            if (old_password.val().trim() == "") {
                alert("请输入旧密码");
                old_password.focus();
                return false;
            }
            if (new_password.val().trim() == "") {
                alert("请输入新密码");
                new_password.focus();
                return false;
            }

            if (new_password.val().trim().length > 8 || new_password.val().trim().length < 6) {
                alert("当前密码不合法, 请重新输入");
                new_password.focus();
                return false;
            }

            if (new_password2.val().trim() == "") {
                alert("请输入确认密码");
                new_password2.focus();
                return false;
            }

            if (new_password.val().trim() != new_password2.val().trim()) {
                alert("新密码两次不一致, 请检查!");
                new_password.focus();
                return false;
            }

            // 冻结提交
            jQuery("#revisePassword").attr("disabled", "disabled");

            jQuery.ajax({
                url: "/user/revisepassword",
                type: 'post',
                data: {
                    "old_password": old_password.val().trim(),
                    'new_password': new_password.val().trim()
                },
                success: function (res) {
                    if (res.code == 200 && res.data == 1) {
                        alert("密码修改成功!");
                        location.href = "myblog_list1.html";

                    } else {
                        alert("修改失败 : " + res.msg);
                        jQuery("#revisePassword").removeAttr("disabled");
                        return false;
                    }
                }
            });
        }

        function myupload() {
            var fileInput = document.getElementById("avatar-file");
            // 1. 获取标签
            // 2. 当点击上传时, 获取 
            // if (fileInput.val().trim() == "") {
            //     alert("请选择要上传的头像");
            //     return false;
            // }

            var file = fileInput.files[0];
            var formData = new FormData();
            formData.append("file", file);

            // 3. 发送请求给后端
            jQuery.ajax({
                url: '/user/upload',
                type: 'post',
                data: formData,
                processData: false,
                contentType: false,
                success: function (res) {
                    if (res.code == 200 && res.data.length > 0 &&
                        res.data.length <= 255) {

                        jQuery("#image").attr("src", res.data);
                        jQuery("#image").show();
                        alert("头像修改成功!");

                    } else {
                        alert("上传失败 : " + res.msg);
                    }
                },


            });
        }

        function initPric() {
            // 初始化个人中心图片
            jQuery.ajax({
                url: '/user/initpric',
                type: 'post',
                data: {},
                success: function (res) {
                    if (res.code == 200 && res.data.image_path.length > 0 &&
                        res.data.image_path.length < 255 && res.data != null) {

                        jQuery("#image").attr("src", res.data.image_path);
                        jQuery("#image").show();

                    } else {
                        // 无需初始化, 原始设定的默认头像
                    }
                }
            });
        }
        initPric();



    </script>

</body>

</html>